package com.yutian.user.demo.controller;

import com.yutian.user.demo.service.UserService;
import com.yutian.user.demo.vo.HttpResponse;
import com.yutian.user.demo.vo.UserAddReq;
import com.yutian.user.demo.vo.UserListReq;
import com.yutian.user.demo.vo.UserUpdateReq;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags="用户管理")
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public HttpResponse add(@RequestBody UserAddReq req){
        long id = userService.add(req);
        return HttpResponse.suc(userService.get(id));
    }

    @PostMapping("/update")
    public HttpResponse update(@RequestBody UserUpdateReq req){
        userService.update(req);
        return HttpResponse.suc(userService.get(req.getId()));
    }

    @GetMapping("/list")
    public HttpResponse list(UserListReq req){
        return HttpResponse.suc(userService.list(req.getNameLike()));
    }

    @PostMapping("/delete")
    public HttpResponse delete(long id){
        userService.delete(id);
        return HttpResponse.suc(null);
    }

    @GetMapping("/detail")
    public HttpResponse get(long id){
        return HttpResponse.suc(userService.get(id));
    }
}
